package com.wtwd.campus.vcom.utils;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCodeEntity;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.vcom.entity.vo.NotDisturb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/4 17:48
 * @Description: 发送指令工具类
 */
@Service("vcomCommandSendUtils")
public class CommandSendUtils {

    private final static Logger logger = LoggerFactory.getLogger(CommandSendUtils.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private IRMFeignClientService irmFeignClientService;

    /**
     * 判断设备是否在线
     *
     * @param imei 设备imei
     * @return 设备是否在线
     */
    public boolean deviceOnline(String imei) {
        return redisService.hasKey(Constants.DEVICE_ONLINE_PREFIX + imei);
    }

    /**
     * 发送免打扰数据
     *
     * @param imei
     * @param notDisturbData
     * @param commandNumber
     * @return
     */
    public boolean sendNotDisturb(String imei, Map<Integer, List<NotDisturb>> notDisturbData, String commandNumber) {
        MqVcomNotDisturb mqVcomNotDisturb = new MqVcomNotDisturb();
        mqVcomNotDisturb.setImei(imei);
        mqVcomNotDisturb.setCommandNumber(commandNumber);
        TreeMap map = new TreeMap<>();
        for (Map.Entry<Integer, List<NotDisturb>> entry : notDisturbData.entrySet()) {
            int key = entry.getKey();
            List<NotDisturb> notDisturbList = entry.getValue();
            StringBuilder sb = new StringBuilder(notDisturbList.size() + ",");
            for (NotDisturb notDisturb : notDisturbList) {
                sb.append(notDisturb.getDatetime().split("-")[0]).append(",").append(notDisturb.getDatetime().split("-")[1]).append(",");
                sb.append(notDisturb.getCallinFlag()).append(",");
                sb.append(notDisturb.getCalloutFlag()).append(",");
            }
            map.put(key, sb.toString());
        }
        mqVcomNotDisturb.setNotDisturbData(map);
        RespCodeEntity respCode = irmFeignClientService.vcomNotDisturb(mqVcomNotDisturb);
        logger.info("免打扰发送到rocketmq结果：result={}", respCode.getCode());
        return respCode.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
    }

    /**
     * @param imei
     * @param commandNumber
     * @param noticeMessage
     * @return
     */
    public boolean sendNoticeMessage(String imei, String commandNumber, VcomNoticeMessage noticeMessage) {
        MqVcomMessage mqVcomMessage = new MqVcomMessage();
        mqVcomMessage.setImei(imei);
        mqVcomMessage.setCommandContent(noticeMessage);
        mqVcomMessage.setCommandNumber(commandNumber);
        RespCodeEntity respCode = irmFeignClientService.vcomNoticeMessage(mqVcomMessage);
        return respCode.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
    }

    public boolean sendShortMessage(String imei, String commandNumber, VcomShortMessage shortMessage) {

        MqVcomMessage mqVcomMessage = new MqVcomMessage();
        mqVcomMessage.setImei(imei);
        mqVcomMessage.setCommandContent(shortMessage);
        mqVcomMessage.setCommandNumber(commandNumber);
        RespCodeEntity respCode = irmFeignClientService.vcomShortMessage(mqVcomMessage);
        return respCode.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
    }


    /**
     * 设置亲情号码
     *
     * @param deviceWhiteCallList
     * @param imei
     * @param commandNumber
     * @return
     */
    public boolean setFamilyNo(List<Map<String, Object>> deviceWhiteCallList, String imei, String commandNumber) {
        MqSetAllWhiteCall mqSetAllWhiteCall = new MqSetAllWhiteCall();
        mqSetAllWhiteCall.setCommandNumber(commandNumber);
        mqSetAllWhiteCall.setImei(imei);
        mqSetAllWhiteCall.setWhiteCallList(deviceWhiteCallList);
        RespCodeEntity respCodeEntity = irmFeignClientService.setAllWhiteCall(mqSetAllWhiteCall);
        boolean result = respCodeEntity.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
        logger.info("Vcom指令【设置亲情号码】发送到RocketMQ : {}", result ? "成功" : "失败");
        return result;
    }

    /**
     * 设置SOS号码
     *
     * @param deviceAllSosCallList
     * @param imei
     * @param commandNumber
     * @return
     */
    public boolean setSOS(List<DeviceSOSList> deviceAllSosCallList, String imei, String commandNumber) {
        MqSetAllSosCall mqSetAllSosCall = new MqSetAllSosCall();
        mqSetAllSosCall.setImei(imei);
        mqSetAllSosCall.setCommandNumber(commandNumber);
        mqSetAllSosCall.setSosCallList(deviceAllSosCallList);
        RespCodeEntity respCodeEntity = irmFeignClientService.setAllSosCallList(mqSetAllSosCall);
        boolean result = respCodeEntity.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
        logger.info("Vcom指令【设置SOS号码】发送到RocketMQ : {}", result ? "成功" : "失败");
        return result;
    }


    /**
     * 设置白名单号码
     *
     * @param deviceWhiteCallList
     * @param imei
     * @param commandNumber
     * @return
     */
    public boolean setWhiteNo(List<Map<String, Object>> deviceWhiteCallList, String imei, String commandNumber) {
        MqSetAllWhiteCall mqSetAllWhiteCall = new MqSetAllWhiteCall();
        mqSetAllWhiteCall.setCommandNumber(commandNumber);
        mqSetAllWhiteCall.setImei(imei);
        mqSetAllWhiteCall.setWhiteCallList(deviceWhiteCallList);
        RespCodeEntity respCodeEntity = irmFeignClientService.setAllWhiteCall(mqSetAllWhiteCall);
        boolean result = respCodeEntity.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
        logger.info("Vcom指令【设置白名单号码】发送到RocketMQ : {}", result ? "成功" : "失败");
        return result;
    }

    /**
     * 恢复出厂设置
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    public boolean reSetDevice(String imei, String commandNumber) {
        RespCodeEntity respCodeEntity = irmFeignClientService.reSetDevice(imei, commandNumber);
        boolean result = respCodeEntity.getCode() == com.wtwd.campus.common.RespCode.SUCCESS.getCode();
        logger.info("Vcom指令【设置白名单号码】发送到RocketMQ : {}", result ? "成功" : "失败");
        return result;
    }

}
